<?php
/*
 * kernel/object.lang.php 
 * 
 * Languages and internationalization 
 * 
 * Copyright (C) 2010, Taras Kudla
 * 
 */

/*
 * Language file format:
 * 
 * key | value
 * or
 * >>> key>>> value <<<
 * 
 */

# language image object
class lang_image{
	private $data=array();
	public function get($key){
		return ( !isset($this->data[$key]) || is_null($this->data[$key]))?false:$this->data[$key];
	}
	public function set($array,&$caller){
		if(get_class($caller)!="lang")return false;
		$this->data=$array;
		return true;
	}
}
# main languages processing object
class lang extends kernel{
	private $SysLang= array();
	public $cache   = array();
	public $parseTime=0;
	/*
	 * FIXME: \n goes as value in lang file
	 */
	private $pattern1 = "/ *([A-z0-9_-]{1,10}) *\| *(.*)/"; # key | value
	private $pattern2 = "/>>>\s*([A-z0-9_-]{1,20})\n*>>>(.+)<<</Us"; # >>> key >>> value <<< (for multiple strings)
	
	# parsing from text
	private function parsedata($data){
		$time=microtime(1);
		$tmp=array();
		$tmp2=array();
		$a=preg_match_all($this->pattern1,$data,$tmp);
		//var_dump($tmp);
		$b=preg_match_all($this->pattern2,$data,$tmp2);
		if( !$a && !$b){
			return false;
		} else {
			$_1=(isset($tmp[1]) && count($tmp[1])>0  )?array_combine(array_values($tmp[1]),array_values($tmp[2])):array();
			$_2=(isset($tmp2[1]) && count($tmp2[1])>0  )?array_combine(array_values($tmp2[1]),array_values($tmp2[2])):array();
			$this->parseTime+=round(microtime(1)-$time,4);
			return array_merge($_1,$_2);
		}
	}
	# getting data
	public function get($s){
		if(!is_object($this->SysLang)){
			$file = $this->core->path()."kernel/lang/".$this->core->language();
			$image=$this->parsefile($file);
			if( !is_object($image) ){
				return false;
			}
			$this->SysLang=&$image;	
		}	
		return $this->SysLang->get($s);
	}
	# parsing from text
	public function parse($text){
		$array = $this->parseData($text);
		if( !$array ){
			return false;
		} else {
			$image = new lang_image();
			$image->set($array,$this);
			return $image;
		}
	}
	# parsing from file and caching implementation
	public function & parsefile($file){
		$false=false;
		# checking if file exists
		if( !file_exists($file)){
			return $false;
		}
		$filehash = md5($file);
		# caching system
		if( $this->core->get("usecache") &&  $this->core->isloaded("cache")){
			$name = "gepard.lang.".$filehash;
			$datename = $name.".date";
			$ctime = $this->core->cache->get($datename);
			if( $ctime ){
				$mtime = filemtime($file);
				if( $mtime > $ctime){
					#need to rebuild cache
					$filedata=file_get_contents($file);
					$langarray = $this->parsedata($filedata);
					if( is_bool($langarray) && !$langarray){
						return $false;
					}
					$this->core->cache->set($datename,time());
					$this->core->cache->set($name,serialize($langarray));
					$this->core->debug("lang","rebuilding cache for langdata: '$file' ");
				}else {
					#returning data from cache
					$filedata = $this->core->cache->get($name);
					$langarray = unserialize($filedata);
					$this->core->debug("lang","getting langdata from cache");
				}
			} else {
				# need to build cache
				$filedata=file_get_contents($file);
				$langarray = $this->parsedata($filedata);
				if( is_bool($langarray) && !$langarray){
						return $false;
				}
				$this->core->cache->set($datename,time());
				$this->core->cache->set($name,serialize($langarray));
				$this->core->debug("view","putting template data into cache");			
			}			
		} else {
			# when caching is disabled
			$filedata=file_get_contents($file);
			$langarray = $this->parsedata($filedata);
			if( is_bool($langarray) && !$langarray){
					return $false;
			}
		}
		$langimage = new lang_image();
		$langimage->set($langarray,$this);
		return $langimage;			
		}

	# setting module language
	public function setModuleLanguage($module,$itlanguages,$default,&$to,$path=null){
		if(in_array($this->core->language(),$itlanguages)){
			$l=$this->core->language();
		}else{
			$l=$default;					
		}
		if( array_key_exists($module,$this->cache) && is_object($this->cache[$module])){
			$to = $this->cache[$module];
			return true;
		}
		if( $path==null){
			$path=$this->core->parseasdir($this->core->get("modsdir")).$module."/lang/";
		} else {
			$path = $this->core->parsepathtosys($path);
		}
		$file=$path.$l;
		$image = $this->parsefile($file);
		if( is_bool($image) && !$image) {
			$image = new lang_image();
		}
		$this->core->debug("LANG","Trying to parse lang file $file");
		$this->cache[$module]=$image;
		$to = $this->cache[$module];
		return true;
	}
	# get mod lang key
	public function getmodlangbykey($module,$key){
		if( array_key_exists($module,$this->cache) && is_object($this->cache[$module])){
			return $this->cache[$module]->get($key);
		} else return null;
	}
	# getting module language object
	public function getlangobj($mod){
		if( array_key_exists($mod,$this->cache) && is_object($this->cache[$mod])){
			return $this->cache[$mod];
		} else return null;		
	}	
}
?>